package com.tools.scanner.ui.history

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.tools.scanner.R
import com.tools.scanner.bean.BarcodeSchema
import com.tools.scanner.database.QrHistory
import com.tools.scanner.databinding.RecyclerQrHistoryItemBinding
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class QrHistoryAdapter(private val onClick: (history: QrHistory) -> Unit) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mHistoryList = mutableListOf<QrHistory>()

    fun refresh(list: List<QrHistory>) {
        mHistoryList.clear()
        mHistoryList.addAll(list)
        notifyDataSetChanged()
    }

    fun deleteItem(qrHistory: QrHistory) {
        for (index in 0 until mHistoryList.size) {
            if (mHistoryList[index].id == qrHistory.id) {
                deleteItem(index)
                return
            }
        }
    }

    fun deleteItem(index: Int) {
        mHistoryList.removeAt(index)
        notifyItemRemoved(index)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding = RecyclerQrHistoryItemBinding.inflate(layoutInflater, parent, false)
        return QrViewHolder(binding, onClick)
    }

    override fun getItemCount(): Int {
        return mHistoryList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is QrViewHolder) {
            holder.refresh(mHistoryList[position])
        }
    }

    private class QrViewHolder(
        private val mBinding: RecyclerQrHistoryItemBinding,
        private val onClick: (history: QrHistory) -> Unit
    ) : RecyclerView.ViewHolder(mBinding.root) {
        private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())

        init {
            mBinding.root.setOnClickListener {
                val history = it.tag
                if (history is QrHistory) {
                    onClick(history)
                }
            }
        }

        fun refresh(history: QrHistory) {
            mBinding.root.tag = history
            mBinding.tvDate.text = dateFormat.format(Date(history.ctime))
            mBinding.tvDetail.text = history.content

            when (history.schema) {
                BarcodeSchema.CALENDER -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_calendar)
                    mBinding.tvType.setText(R.string.scan_type_calendar)
                }

                BarcodeSchema.CONTACT -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_contact)
                    mBinding.tvType.setText(R.string.scan_type_contact)
                }

                BarcodeSchema.EMAIL -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_email)
                    mBinding.tvType.setText(R.string.scan_type_email)
                }

                BarcodeSchema.GEO -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_location)
                    mBinding.tvType.setText(R.string.scan_type_geo)
                }

                BarcodeSchema.PHONE -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_phone)
                    mBinding.tvType.setText(R.string.scan_type_phone)
                }

                BarcodeSchema.SMS -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_sms)
                    mBinding.tvType.setText(R.string.scan_type_sms)
                }

                BarcodeSchema.URL -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_link)
                    mBinding.tvType.setText(R.string.scan_type_link)
                }

                BarcodeSchema.WIFI -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_wifi)
                    mBinding.tvType.setText(R.string.scan_type_wifi)
                }

                BarcodeSchema.EAN_8 -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.ean_8)
                }

                BarcodeSchema.EAN_13 -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.ean_13)
                }

                BarcodeSchema.UPC_E -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.upc_e)
                }

                BarcodeSchema.UPC_A -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.upc_a)
                }

                BarcodeSchema.CODE_39 -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.code_39)
                }

                BarcodeSchema.CODE_93 -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.code_93)
                }

                BarcodeSchema.CODE_128 -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.code_128)
                }

                BarcodeSchema.ITF -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.itf)
                }

                BarcodeSchema.PDF_417 -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.PDF417)
                }

                BarcodeSchema.CODABAR -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.codabar)
                }

                BarcodeSchema.DATA_MATRIX -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.data_matrix)
                }

                BarcodeSchema.AZTEC -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_barcode)
                    mBinding.tvType.setText(R.string.aztec)
                }

                else -> {
                    mBinding.ivType.setImageResource(R.drawable.ic_text)
                    mBinding.tvType.setText(R.string.scan_type_content)
                }
            }
        }

    }
}